package Kuaishou;

import java.util.List;

import com.google.common.collect.Lists;

import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 不能这样用，相互引用，会导致栈溢出
 *
 * @author leiwenkang <leiwenkang@kuaishou.com>
 * Created on 2020-11-03
 */
public class Test12相互引用 {
    public static void main(String[] args) {
        Teacher teacher = new Teacher(1);

        List<Student> students = Lists.newArrayList();
        Student student1 = new Student(1);
        students.add(student1);
        Student student2 = new Student(2);
        students.add(student2);

        student1.setStudents(students);
        student2.setStudents(students);

        teacher.setStudents(Lists.newArrayList(student1, student2));

        System.out.println(teacher);
        System.out.println(student1);
        System.out.println(student1.getStudents());
        System.out.println(student2);
    }
}

@Data
class Student extends Teacher {
    private int id;

    public Student(int id) {
        this.id = id;
    }
}

@Data
@NoArgsConstructor
class Teacher {
    private int id;
    private List<Student> students;

    public Teacher(int id) {
        this.id = id;
    }
}
